﻿//-----------------------------------------------------------------------
// <copyright file="CommandID.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Editor.Command
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Class which encapsulates a command that tells the editor to do something.
    /// </summary>
    public class CommandId
    {
        #region Fields - Private static

        /// <summary>
        /// Set which contains the names of commands that have been used.
        /// </summary>
        private static ISet<string> usedCommandNamesSet = new HashSet<string>();

        #endregion

        #region Constructor

        /// <summary>
        /// Command ID constructor.
        /// </summary>
        /// <param name="name">Name of the command.</param>
        private CommandId(string name)
        {
            this.Name = name;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets the name of the command.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        #endregion

        #region Methods - Public static

        /// <summary>
        /// Create a command with the passed name.
        /// 
        /// If the name is a duplicate, an ArgumentException() will be thrown.
        /// </summary>
        /// <param name="name">Name of the command to create.</param>
        /// <returns>Returns a command instance with the passed name.</returns>
        public static CommandId Create(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Name must not be null or empty.");
            }

            if (usedCommandNamesSet.Contains(name))
            {
                throw new ArgumentException("Name " + name + " is already used.", "name");
            }

            usedCommandNamesSet.Add(name);

            return new CommandId(name);
        }

        #endregion
    }
}
